using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LibreriaWC
{
    [ParseChildren(true)]
    [PersistChildren(false)]
    [ToolboxData("<{0}:ContenedorNew runat=server></{0}:ContenedorNew>")]
    public class ContenedorNew : Panel
    {
        public ContenedorNew() : base()
        {
        }

        private Panel container;

        public Control Container
        {
            get
            {
                return container;
            }
        }

        private ITemplate _topTemplate;

        [TemplateInstance(TemplateInstance.Single)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Browsable(false)]
        public ITemplate TopTemplate
        {
            get { return _topTemplate; }
            set
            {
                _topTemplate = value;
            }
        }

        private ITemplate _formularioTemplate;

        [TemplateInstance(TemplateInstance.Single)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Browsable(false)]
        public ITemplate FormularioTemplate
        {
            get { return _formularioTemplate; }
            set
            {
                _formularioTemplate = value;
            }
        }

        private ITemplate _botoneraTemplate;

        [TemplateInstance(TemplateInstance.Single)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Browsable(false)]
        public ITemplate BotoneraTemplate
        {
            get { return _botoneraTemplate; }
            set
            {
                _botoneraTemplate = value;
                EnsureChildControls();
            }
        }

        private Table tabla0;
        private Table tabla1;
        private Table tabla2;
        private TableRow tr;
        private TableCell tc;
        private TableCell tcContenido;
        private Image imagenBorderInternoIzq;
        private Image imagenBorderInternoDer;

        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ConSolapas
        {
            get
            {
                if (ViewState["ConSolapas"] == null)
                    return false;
                else
                    return Convert.ToBoolean(ViewState["ConSolapas"]);
            }

            set
            {
                ViewState["ConSolapas"] = value;

                if (value)
                {
                    imagenBorderInternoIzq.Height = Unit.Pixel(455);
                    imagenBorderInternoDer.Height = Unit.Pixel(455);
                    tabla1.CssClass = "griscentrosolapas";

                    if (tcContenido.FindControl("imgEspacioConSolapas") == null)
                    {
                        /*
                        Image imagenEspacio = new Image();
                        imagenEspacio.ID = "imgEspacioConSolapas";
                        imagenEspacio.ImageUrl = (!this.DesignMode ? HttpContext.Current.Request.ApplicationPath + "/Images/trans.gif" : "");
                        imagenEspacio.Height = Unit.Pixel(15);
                        tcContenido.Controls.Add(imagenEspacio);
                        */
                    }
                }
                else
                {
                    imagenBorderInternoIzq.Height = Unit.Pixel(470);
                    imagenBorderInternoDer.Height = Unit.Pixel(470);
                    tabla1.CssClass = "griscentro";
                }
            }
        }

        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ConDescripcionObra
        {
            get
            {
                if (ViewState["ConDescripcionObra"] == null)
                    return false;
                else
                    return Convert.ToBoolean(ViewState["ConDescripcionObra"]);
            }

            set
            {
                ViewState["ConDescripcionObra"] = value;

                if (tabla2 != null)
                    tabla2.Visible = value;
            }
        }

        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public bool MostrarImagenTop
        {
            get
            {
                if (ViewState["MostrarImagenTop"] == null)
                    return true;
                else
                    return Convert.ToBoolean(ViewState["MostrarImagenTop"]);
            }

            set
            {
                ViewState["MostrarImagenTop"] = value;
            }
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            container = new Panel();

            tabla0 = new Table();
            tabla0.CellPadding = 0;
            tabla0.CellSpacing = 0;

            tr = new TableRow();
            tabla0.Rows.Add(tr);

            imagenBorderInternoIzq = new Image();
            imagenBorderInternoIzq.ImageUrl = (!this.DesignMode ? HttpContext.Current.Request.ApplicationPath + "/Images/BorderInternoIzq.jpg" : "");
            imagenBorderInternoIzq.Width = Unit.Pixel(2);

            if (ConSolapas)
                imagenBorderInternoIzq.Height = Unit.Pixel(455);
            else
                imagenBorderInternoIzq.Height = Unit.Pixel(470);

            tc = new TableCell();
            tc.Controls.Add(imagenBorderInternoIzq);
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.CssClass = "FondoContenido";

            tabla1 = new Table();
            tc.Controls.Add(tabla1);

            if (BotoneraTemplate != null)
            { BotoneraTemplate.InstantiateIn(tc); }

            tr.Cells.Add(tc);

            imagenBorderInternoDer = new Image();
            imagenBorderInternoDer.ImageUrl = (!this.DesignMode ? HttpContext.Current.Request.ApplicationPath + "/Images/BorderInternoDerecho.jpg" : "");
            imagenBorderInternoDer.Width = Unit.Pixel(2);

            if (ConSolapas)
                imagenBorderInternoDer.Height = Unit.Pixel(455);
            else
                imagenBorderInternoDer.Height = Unit.Pixel(470);

            tc = new TableCell();
            tc.Controls.Add(imagenBorderInternoDer);
            tr.Cells.Add(tc);

            tabla1.CellSpacing = 0;
            tabla1.CellPadding = 0;
            tabla1.HorizontalAlign = HorizontalAlign.Center;

            if (ConSolapas)
                tabla1.CssClass = "griscentrosolapas";
            else
                tabla1.CssClass = "griscentro";

            tr = new TableRow();
            tabla1.Rows.Add(tr);

            tc = new TableCell();
            tcContenido = tc;
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.VerticalAlign = VerticalAlign.Top;

            if (ConSolapas)
            {
                /*
                Image imagenEspacio = new Image();
                imagenEspacio.ID = "imgEspacioConSolapas";
                imagenEspacio.ImageUrl = (!this.DesignMode ? HttpContext.Current.Request.ApplicationPath + "/Images/trans.gif" : "");
                imagenEspacio.Height = Unit.Pixel(15);
                tc.Controls.Add(imagenEspacio);
                */
            }

            tr.Cells.Add(tc);

            if (FormularioTemplate != null)
            { FormularioTemplate.InstantiateIn(tc); }

            container.Controls.Add(tabla0);

            Controls.Add(container);

            base.CreateChildControls();
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            base.RenderBeginTag(writer);

            Image imagen = new Image();

            if (MostrarImagenTop)
            {
                imagen.ImageUrl = (!this.DesignMode ? HttpContext.Current.Request.ApplicationPath + "/Images/gris-interior2.jpg" : "");
                imagen.Height = Unit.Pixel(15);
                writer.Write("<tr><td align=\"center\">");
                imagen.RenderControl(writer);
                writer.Write("</td></tr>");
            }

            writer.Write("<tr>");
            writer.Write("<td align=\"center\">");
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.Write("</td>");
            writer.Write("</tr>");

            Image imagen = new Image();
            imagen.ImageUrl = (!this.DesignMode ? HttpContext.Current.Request.ApplicationPath + "/Images/gris-interior-inf2.jpg" : "");
            writer.Write("<tr><td align=\"center\">");
            imagen.RenderControl(writer);
            writer.Write("</td></tr>");

            base.RenderEndTag(writer);
        }
    }
}
